<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: GetCircle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\msg;


use app\api\model\dao\DashangRecord;
use app\api\model\dao\MsgRecords;
use app\api\model\dao\MsgUser;
use app\api\model\dao\NotePraise;
use app\api\model\dao\NoteReply;
use app\api\model\dao\UserAccount;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Exception;
use think\Validate;

class GetMsgList extends PageBase
{

    private $objNotePraise;
    private $objNoteReply;
    private $objNoteDashang;
    private $objMsgUser;
    private $objMsgRecords;
    private $objUserAccount;

    public function __construct()
    {
        parent::__construct();

        $this->objNotePraise = new NotePraise();
        $this->objNoteReply = new NoteReply();
        $this->objNoteDashang = new DashangRecord();
        $this->objMsgUser = new MsgUser();
        $this->objMsgRecords = new MsgRecords();
        $this->objUserAccount = new UserAccount();

    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'userid' => 'require',
        ];
        $msg = [
            'userid.require' => '请选择聊天的对象',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    /**
     * page 入口
     *
     * @param $param
     * @return array
     * @throws Exception
     */
    public function excute($param)
    {
        $ret = $this->checkToken($param);
        if (false === $ret) {
            throw new Exception('check token fail...', Error::ERR_NO_AUTH);
        }

        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }

        $content_ret = $this->objMsgUser->getMsg($this->user_id, $param['userid'], ['user_id', 'recv_user_id', 'content'], $this->offset, $this->limit);

        foreach ($content_ret as $val) {
            $this->data[] = [
                'userid' => $val['user_id'],
                'content' => $val['content'],
            ];
        }

        return $this->getRes();
    }

}
